package main;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by francis on 2016/11/6.
 */
public class BaiduSongs {
    private BaiduSongs() {
    }

    public static BaiduSongs instance = new BaiduSongs();

    public String getMobileUserAgent(long counter) {
        return "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36."+counter;
    }

    public String getPCUserAgent() {
        return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36";
    }

    public List<String> getTags() {
        String tagUrl = "http://music.baidu.com/tags";
        Connection con = Jsoup.connect(tagUrl);
        con.header("User-Agent", getMobileUserAgent(0));
        Connection.Response rs = null;//获取响应
        try {
            rs = con.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Document d1 = Jsoup.parse(rs.body());
        Elements elements = d1.body().getElementsByClass("tag-cloud");
        List<String> tags = new ArrayList<String>();
        if (elements != null && !elements.isEmpty()) {
            for (Element e : elements) {
                Elements tagsElements = e.getElementsByClass("need-active");
                if (tagsElements != null && !tagsElements.isEmpty()) {
                    for (Element e1 : tagsElements) {
                        String str = e1.text();
                        if (str != null && str.length() <= 0) {
                            continue;
                        }
                        tags.add(str);
                    }
                }
            }
        }
        return tags;
    }

    public List<Long> getSongsFromTag(String tagsName) {
        String tagUrl = "http://music.baidu.com/tag/" + tagsName;
        Connection con = Jsoup.connect(tagUrl);
        con.header("User-Agent", getMobileUserAgent(0));
        Connection.Response rs = null;//获取响应
        try {
            rs = con.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Document d1 = Jsoup.parse(rs.body());
        Elements elements = d1.body().getElementsByClass("list");
        if (elements == null || elements.isEmpty()) {
            return null;
        }
        Elements songLis = elements.get(0).children();
        if (songLis == null || songLis.isEmpty()) {
            return null;
        }
        List<Long> songs = new ArrayList<Long>();

        for (Element e : songLis) {
            String songId = e.attr("data-sid");
            if (songId == null || songId.length() <= 0) {
                continue;
            }
            Long id = 0L;
            try {
                id = Long.valueOf(songId);
            } finally {
                if (id > 0) {
                    songs.add(id);
                }
            }
        }
        boolean hasMoere = true;
        long counter=0;
        do {
            counter++;

            String strMore = String.format("http://music.baidu.com/loadmore/tag/?method=baidu.ting.tag.songlist&tagname=%s&limit=20&offset=%d&v=%d", tagsName, counter*20 , counter);
            con = Jsoup.connect(tagUrl);
            con.header("Content-Type", "application/json; charset=utf-8");
            con.header("User-Agent", getMobileUserAgent(counter));
            con.referrer("http://music.baidu.com/tag/" + tagsName + "/");
            con.header("X-Requested-With", "XMLHttpRequest");
            Connection.Response rs1 = null;//获取响应
            try {
                rs1 = con.execute();
            } catch (IOException e) {
                e.printStackTrace();
                hasMoere = false;
                continue;
            }
            if (rs1 == null) {
                hasMoere = false;
                continue;
            }
            d1 = Jsoup.parse(rs1.body());
            Elements moreSongs = d1.getElementsByAttribute("data-sid");
            if (moreSongs == null || moreSongs.isEmpty()) {
                hasMoere = false;
            }

            for (Element moreSong : moreSongs) {
                Long str = Long.valueOf(moreSong.attr("data-sid"));
                System.out.println(String.format("No.%d", counter));
                songs.add(str);
                System.out.println(str);
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        while (hasMoere);
        return songs;
    }
}
